//
//  JYTTrainDetailViewController.m
//  JYT_IOS
//
//  Created by nxa-chengjinhui on 2017/1/12.
//  Copyright © 2017年 nxa-yuanguoxiang. All rights reserved.
//

#import "JYTTrainDetailViewController.h"
#import "JYTButton.h"
#import "global.h"
#import "BGHomePageTopGoodsCell.h"
#import "BGTopSilderBar.h"
#import "NXANonIssueModel.h"
#define HIG CURRENT_SCREEN_HEIGHT*3/10
@interface JYTTrainDetailViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    UIImageView *headImg;
    UIView *view1;
    NSString *str1;//类型
    NSString *str2;//科目
}
@property(nonatomic,weak)BGTopSilderBar* silderBar;
@property(nonatomic,weak)UICollectionView* collectView;
@property(nonatomic,assign)int currentBarIndex;

@property(nonatomic,strong)NSMutableArray *sliderItemArr;    //!<顶部滑动天数的数据源
//@property(nonatomic,strong)NXASingleDataManager *dataManager;    //!<
@end

@implementation JYTTrainDetailViewController

-(instancetype)initWithModel:(JYTAppointModel *)model{
    if (self=[super init]) {
        self.model = model;
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
   
    self.view.backgroundColor = [UIColor whiteColor];
    
    NSDictionary *dic = @{@"lftBtnBgImg":@"ic_goback",
                          @"lftBtnTitle":@"no",
                          @"pageTitle":@"教练详情",
                          @"rhtBtnBgImg":@"",
                          @"rhtBtnTitle":@"no"};
    
    [self setNavgationBarWithDic:dic];

    MyLog(@"self.model%@",self.model);
    //详细信息展示
    [self detailShow];
    //预约详情
    [self appointDeatail];
}

-(void)detailShow{
    //图片信息
    UIImageView *picImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, CURRENT_SCREEN_WIDTH, HIG)];
//    [picImg sd_setImageWithURL:[NSURL URLWithString:self.model.Url] placeholderImage:[UIImage imageNamed:@""]];
    picImg.image = [UIImage imageNamed:@"02驾校查询_22"];
    [self.view addSubview:picImg];
    //教练个人信息
    UILabel *nameLab = [[UILabel alloc] initWithFrame:CGRectMake(20, 64 + HIG-70, 60, 30)];
    nameLab.text = self.model.coach_name;
    nameLab.textColor = [UIColor whiteColor];
    nameLab.font = [UIFont boldSystemFontOfSize:18];
    nameLab.adjustsFontSizeToFitWidth = YES;
    nameLab.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:nameLab];
    //性别
    UIImageView *sexImg = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(nameLab.frame)+10, CGRectGetMaxY(nameLab.frame)-28, 20, 20)];
    sexImg.image = [UIImage imageNamed:@"ic_home_yx"];
    [self.view addSubview:sexImg];
    
    UIView *starView = [[UIView alloc] initWithFrame:CGRectMake(5, CGRectGetMaxY(nameLab.frame), 110, 25)];
    [self.view addSubview:starView];
    
    for (int i=0; i<5; i++) {
        UIImageView *star = [[UIImageView alloc] initWithFrame:CGRectMake(5+20*i, 3, 20, 18)];
        star.image = [UIImage imageNamed:@"02驾校查询_29"];
        [starView addSubview:star];

        if (i<[self.model.grade_value integerValue]) {
            star.image = [UIImage imageNamed:@"02驾校查询_27"];
        }
    }
    //驾校名称
    UIButton *trainSchoosl = [UIButton buttonWithType:UIButtonTypeCustom];
    trainSchoosl.frame = CGRectMake(CGRectGetMaxX(starView.frame)+10, CGRectGetMaxY(nameLab.frame)-20, CURRENT_SCREEN_WIDTH/4, 20);
    [trainSchoosl setTitle:self.model.driving_school_name forState:UIControlStateNormal];
    [trainSchoosl setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    trainSchoosl.titleLabel.font = [UIFont boldSystemFontOfSize:14];
    trainSchoosl.titleLabel.textAlignment = NSTextAlignmentLeft;
    trainSchoosl.titleLabel.adjustsFontSizeToFitWidth = YES;
    [trainSchoosl addTarget:self action:@selector(getSchoolDetaiClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:trainSchoosl];
    //教学人数
    UILabel *numLab = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(starView.frame)+16, starView.frame.origin.y, CURRENT_SCREEN_WIDTH/4, 25)];
    numLab.text = [NSString stringWithFormat:@"已教555%ld学员",[self.model.grade_value integerValue]];
    numLab.textColor = [UIColor grayColor];
    numLab.font = [UIFont boldSystemFontOfSize:14];
    numLab.adjustsFontSizeToFitWidth = YES;
    [self.view addSubview:numLab];
    //教练头像
    headImg = [[UIImageView alloc] initWithFrame:CGRectMake(CURRENT_SCREEN_WIDTH-20-CURRENT_SCREEN_WIDTH/5, CGRectGetMaxY(picImg.frame)-CURRENT_SCREEN_WIDTH/10, CURRENT_SCREEN_WIDTH/5, CURRENT_SCREEN_WIDTH/5)];
    [headImg sd_setImageWithURL:[NSURL URLWithString:self.model.Url] placeholderImage:[UIImage imageNamed:@"tx_w"]];
    headImg.layer.masksToBounds = YES;
    headImg.layer.cornerRadius = CURRENT_SCREEN_WIDTH/10;
    [self.view addSubview:headImg];
}

-(void)appointDeatail{
    
    view1 = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(headImg.frame)+5, CURRENT_SCREEN_WIDTH, 30)];
//    view1.backgroundColor = [UIColor orangeColor];
    view1.layer.borderWidth = 0.5;
    view1.layer.borderColor = [[UIColor grayColor] CGColor];
    [self.view addSubview:view1];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(CURRENT_SCREEN_WIDTH/2, 0, 1, 30)];
    line.backgroundColor = [UIColor grayColor];
    [view1 addSubview:line];
    
    NSArray *arr1 = [NSArray arrayWithObjects:@"手动挡(C1)",@"自动挡(C2)",@"科目二",@"科目三", nil];
    
    for (int i=0; i<4; i++) {
        JYTButton *btn = [JYTButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i*CURRENT_SCREEN_WIDTH/4, 5, CURRENT_SCREEN_WIDTH/4-5, 20);
        [btn setImage:[UIImage imageNamed:@"dl_8"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"dl_9"] forState:UIControlStateSelected];
        [btn setTitle:arr1[i] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        btn.titleLabel.adjustsFontSizeToFitWidth = YES;
        btn.tag = 1000+i;
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnClicked:) forControlEvents:UIControlEventTouchUpInside];
        [view1 addSubview:btn];
        
        if (i==0 || i==2) {
            btn.selected = YES;
        }
    }
    
//    view2 = [[UIView alloc] initWithFrame:CGRectMake(CURRENT_SCREEN_WIDTH/2, CGRectGetMaxY(headImg.frame)+5, CURRENT_SCREEN_WIDTH/2, 30)];
//    view2.layer.borderWidth = 0.5;
////    view2.backgroundColor = [UIColor greenColor];
//    view2.layer.borderColor = [[UIColor grayColor] CGColor];
//    [self.view addSubview:view2];
    
//    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(view1.frame)+5, CURRENT_SCREEN_WIDTH, CURRENT_SCREEN_HEIGHT-CGRectGetMaxY(view1.frame)-5)];
//    view.backgroundColor = [UIColor blueColor];
//    [self.view addSubview:view];
    
    str1 = @"手动挡(C1)";
    str2 = @"科目二";
    
    [self initSilderBar];
    [self initCollectView];
}

/**
 初始化BGTopSilderBar
 */
-(void)initSilderBar{
    BGTopSilderBar* silder = [[BGTopSilderBar alloc] initWithFrame:CGRectMake(0,0,CURRENT_SCREEN_WIDTH, 50)];
    silder.items = self.sliderItemArr;
    silder.contentCollectionView = _collectView;//_collectView必须要在前面初始化,不然这里值为nil
    _silderBar = silder;
    [self.view addSubview:silder];
}

/**
 初始化底部滑动的collectionView
 */
-(void)initCollectView{
    CGFloat Margin = 0;
    CGFloat W = CURRENT_SCREEN_WIDTH;
    CGFloat H = CURRENT_SCREEN_HEIGHT-75;
    CGRect rect = CGRectMake(Margin,CGRectGetMaxY(view1.frame), W,CURRENT_SCREEN_HEIGHT-CGRectGetMaxY(view1.frame));
    //初始化布局类(UICollectionViewLayout的子类)
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.itemSize = CGSizeMake(W, H);
    layout.minimumInteritemSpacing = 0;//设置行间隔
    layout.minimumLineSpacing = 0;//设置列间隔
    //初始化collectionView
    UICollectionView* collectView = [[UICollectionView alloc]initWithFrame:rect collectionViewLayout:layout];
    collectView.tag = 0;
    collectView.backgroundColor = [UIColor clearColor];
    _collectView = collectView;
    //设置代理
    collectView.delegate = self;
    collectView.dataSource = self;
    collectView.showsHorizontalScrollIndicator = NO;
    // 注册cell
    [collectView registerNib:[UINib nibWithNibName:@"BGTopSilderBarCell" bundle:nil] forCellWithReuseIdentifier:@"BGTopSilderBarCell"];
    //设置水平方向滑动
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //设置分页
    collectView.pagingEnabled = YES;
    [self.view addSubview:collectView];
}


#pragma -- UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.sliderItemArr.count;//此处页面的张数要跟顶部滑动栏的item个数一样
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BGHomePageTopGoodsCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"BGTopSilderBarCell" forIndexPath:indexPath];
    cell.backgroundColor = color(rand()%255,rand()%255,rand()%255,1.0);
    
    cell.date = self.sliderItemArr[indexPath.item];
    
    return cell;
}


-(void)getSchoolDetaiClicked{
    MyLog(@"驾校详情");
}

-(void)btnClicked:(UIButton *)bttn {
//    if (bttn.selected==YES) {
//        bttn.userInteractionEnabled = NO;
//    }else{
//        bttn.userInteractionEnabled = YES;
//        
        //选择手动 自动
        if (bttn.tag==1001) {
            bttn.selected = YES;
            UIButton *btn = [view1 viewWithTag:1000];
            btn.selected = NO;
            str1 = bttn.titleLabel.text;
        }else if (bttn.tag==1000){
            bttn.selected = YES;
            UIButton *btn = [view1 viewWithTag:1001];
            btn.selected = NO;
            str1 = bttn.titleLabel.text;
        }else if (bttn.tag==1003){//选择科目
            bttn.selected = YES;
            UIButton *btn = [view1 viewWithTag:1002];
            btn.selected = NO;
            str2 = bttn.titleLabel.text;
        }else if (bttn.tag==1002){
            bttn.selected = YES;
            UIButton *btn = [view1 viewWithTag:1003];
            btn.selected = NO;
            str2 = bttn.titleLabel.text;
        }
//    }
    MyLog(@"%@%@",str1,str2);
}
- (NSMutableArray *)sliderItemArr
{
    if (_sliderItemArr == nil) {
        _sliderItemArr = [NSMutableArray array];
    }
    return _sliderItemArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
